/**
 @Name：UI启动入口
 @Author：蓝博
*/

layui.extend({
    setter: layui.cache.modelPath + 'setter',
    admin: layui.cache.modelPath + 'admin',
    view: layui.cache.modelPath + 'view'
}).define(['setter', 'element', 'admin'], function (exports) {
    var $ = layui.jquery,
        setter = layui.setter,
        element = layui.element,
        admin = layui.admin;

    //启动Loading
    admin.showLoading();

    //入口页面
    function entryPage() {
        var router = layui.router(),
            container = admin.view(setter.layoutDom),
            pathURL = "/" + router.path.join('/');

        //独立页面
        if (admin.indPage(pathURL)) {
            container.render(pathURL).done(function () {
                admin.pageType = 'alone';
                admin.regRouter([pathURL]);
                //延时200毫秒删除Loading
                setTimeout(function () {
                    admin.removeLoading();
                }, 200);
            });
        } else {
            //强制拦截未登入

            //渲染主体页面
            container.render(setter.viewDefault.layout).done(function () {
                //侧边栏
                layui.data.nav = function (res) {
                    element.render('nav');
                    admin.regRouter(res);
                    admin.loadHome({
                        url: setter.viewDefault.entry
                    });
                }
                //延时200毫秒删除Loading
                setTimeout(function () {
                    admin.removeLoading();
                }, 200);
            });
        }

        //将模块根路径设置为视图逻辑目录
        layui.config({
            base: setter.base + layui.cache.viewModelPath
        });
    };

    //初始主体结构
    layui.link(
        '/css/main.css?v=' + layui.cache.version,
        function () {
            entryPage();
        },
        'easyxin'
    );

    //监听Hash改变
    window.onhashchange = function () {
        entryPage();
        layui.event.call(this, setter.modName, 'hash({*})', layui.router());
    };

    //扩展第三方模块
    layui.each(setter.extend, function (index, item) {
        var mods = {};
        mods[item] = '{/}' + setter.base + layui.cache.modelPath + 'extend/' + item;
        layui.extend(mods);
    });

    //对外输出接口
    exports('startup', {});
});
